import { getRandom } from "./util.js";

class Food{
    constructor({x = 0, y = 0, width = 20, height = 20, color = 'green'} = {}){
        //食物存储器
        this.elements = []

        this.x = x
        this.y = y
        this.width = width
        this.height = height
        this.color = color
    }

    //渲染函数
    render(map){
        //删除之前创建的食物
        this.remove()

        //map格子化
        this.x = getRandom(0, map.offsetWidth / this.width - 10) * this.width
        this.y = getRandom(0, map.offsetHeight / this.height - 10) * this.height

        let div = document.createElement('div')
        map.appendChild(div)

        this.elements.push(div)

        //设置div的样式
        div.style.position = 'absolute'
        div.style.left = this.x + 'px'
        div.style.top = this.y + 'px'
        div.style.width = this.width + 'px'
        div.style.height = this.height + 'px'
        div.style.background = this.color
    }

    remove(){
        for(let i = this.elements.length - 1; i >= 0; i--){
            //删除真实的dom
            this.elements[i].parentNode.removeChild(this.elements[i])
            //删除数组中的元素
            this.elements.splice(i, 1)
        }
    }
}

export default Food